package com.itheima.app.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.im.HuanXinTemplate;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Friend;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.FriendService;
import com.itheima.vo.ContactVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserCommentVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class MessageManager {

    @DubboReference
    UserInfoService userInfoService;

    @DubboReference
    QuestionService questionService;

    @Autowired
    HuanXinTemplate huanXinTemplate;

    @DubboReference
    private FriendService friendService;

    @DubboReference
    private CommentService commentService;

    public ResponseEntity replyStrangerQuestions(Long recommendUserId, String reply) {
        //1.获取当前登陆者
        Long userId = UserHolder.get().getId();

        //2.获取当前登陆者的userInfo
        UserInfo userInfo = userInfoService.findByUserId(userId);

        //3.获取推荐用户的陌生人问题
        Question question = questionService.findByUserId(recommendUserId);
        if (null == question) {
            question = new Question();
        }

        //4.封装回复信息
        /*
            userId:登陆者id
            huanXinId:登陆者环信id
            nickname:登陆者昵称
            strangerQuestion:推荐用户的陌生人问题
            reply:登陆者回复的内容
         */
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("huanXinId", "HX" + userId);
        map.put("nickname", userInfo.getNickname());
        map.put("strangerQuestion", question.getStrangerQuestion());
        map.put("reply", reply);

        //5.调用环信模块发送信息
        huanXinTemplate.sendMsg("HX" + recommendUserId, JSON.toJSONString(map));

        return ResponseEntity.ok(null);
    }

    // 添加好友
    public void addContact(Long friendId) {
        //1.获取当前登陆者id
        Long userId = UserHolder.get().getId();

        //2.调用friendService添加好友
        friendService.addContact(userId, friendId);

        //3.调用环信模版添加好友
        huanXinTemplate.addContacts("HX" + userId, "HX" + friendId);
    }

    // 查看好友列表
    public ResponseEntity findContactsByPage(Integer pageNum, Integer pageSize) {
        //1.获取登陆者id
        Long userId = UserHolder.get().getId();

        //2.远程调用service查询好友分页数据
        PageBeanVo pageBeanVo = friendService.findContactsByPage(userId, pageNum, pageSize);

        //3.获取分页数据中好友list,遍历list,获取每个好友id,查询好友userInfo,将每个userInfo封装成ContactVo(HXid),将每个vo放入voList中
        List<ContactVo> voList = new ArrayList<>();

        List<Friend> items = (List<Friend>) pageBeanVo.getItems();
        if (CollUtil.isNotEmpty(items)) {
            for (Friend friend : items) {
                //获取每个好友id,查询好友userInfo,
                Long friendId = friend.getFriendId();
                UserInfo userInfo = userInfoService.findByUserId(friendId);

                //将每个userInfo封装成ContactVo(HXid),将每个vo放入voList中
                ContactVo vo = new ContactVo();
                vo.setUserInfo(userInfo);
                //封装环信id
                vo.setUserId("HX" + friendId);

                voList.add(vo);
            }
        }

        //4.给pageBeanVo设置list,且返回
        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findCommentsByPage(Integer pageNum, Integer pageSize, Integer commentType) {
        //1.获取当前登陆者id
        //2.远程调用service查询pageBeanVo
        PageBeanVo pageBeanVo = commentService.findByPage(UserHolder.get().getId(), commentType, pageNum, pageSize);

        //3.获取pageBeanvo中集合(comment),只有操作者id,遍历集合,获取每个操作者userInfo,将userInfo和comment封装成页面上需要的数据,将每个vo对象放入voList集合(先创建处理)中
        List<UserCommentVo> voList = new ArrayList<>();

        List<Comment> items = (List<Comment>) pageBeanVo.getItems();

        if (CollUtil.isNotEmpty(items)) {
            items.forEach(e -> {
                //获取每个操作者userInfo
                UserInfo userInfo = userInfoService.findByUserId(e.getUserId());

                //将userInfo和comment封装成页面上需要的数据对象
                UserCommentVo vo = new UserCommentVo();
                vo.setId(userInfo.getId().toString());//可以写用户id 也可以写评论
                vo.setAvatar(userInfo.getAvatar());
                vo.setNickname(userInfo.getNickname());
                vo.setCreateDate(DateUtil.formatDateTime(new Date(e.getCreated())));//创建时间

                voList.add(vo);
            });
        }

        //4.将voList设置给pageBeanVo
        pageBeanVo.setItems(voList);

        //5.返回pageBeanvo
        return ResponseEntity.ok(pageBeanVo);
    }

}
